package io.renren.modules.shop.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.R;
import io.renren.modules.shop.mapper.ShopManageMapper;
import io.renren.modules.shop.model.ShopManage;
import io.renren.modules.shop.service.ShopManageService;
import io.renren.modules.sys.dao.SysUserRoleDao;
import io.renren.modules.sys.entity.SysRoleEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysRoleService;
import io.renren.modules.sys.service.SysUserService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author A夜兔A
 * @data 2021年11月16日 13:13
 */
@Service
@Transactional
public class ShopManageServiceImpl extends ServiceImpl<ShopManageMapper, ShopManage> implements ShopManageService {

    @Autowired
    private ShopManageMapper shopManageMapper;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserRoleDao sysUserRoleDao;
    @Override
    public R addShop(ShopManage shopManage) {
        SysUserEntity one = sysUserService.getOne(new LambdaQueryWrapper<SysUserEntity>()
                .eq(SysUserEntity::getUserId, shopManage.getUserId()));
        if (StringUtils.isEmpty(one))return R.error(500,"绑定的账户无效");
        List<ShopManage> shopManages = shopManageMapper.selectList(new LambdaQueryWrapper<ShopManage>()
                .eq(ShopManage::getUserId, shopManage.getUserId()));
        if (shopManages.size()>0)return R.error(500,"该账号已经绑定商户");
        List<ShopManage> shopNums = shopManageMapper.selectList(new LambdaQueryWrapper<ShopManage>()
                .eq(ShopManage::getShopNum, shopManage.getShopNum()));
        if (shopNums.size()>0)return R.error(500,"该编号已经被使用");
        shopManageMapper.createTable(shopManage.getShopNum()+"_customers");
        shopManageMapper.createRecord(shopManage.getShopNum()+"_record");
        shopManageMapper.insert(shopManage);
        return R.ok("操作成功");
    }

    @Override
    public R delShop(Integer shopId) {
        ShopManage shopManage = shopManageMapper.selectOne(new LambdaQueryWrapper<ShopManage>()
                .eq(ShopManage::getShopId, shopId));

        shopManageMapper.delTable(shopManage.getShopNum()+"_customers");
        shopManageMapper.delTable(shopManage.getShopNum()+"_record");
        shopManageMapper.deleteById(shopId);
        return R.ok("操作成功");
    }

    @Override
    public R getShopMessage() {
        SysUserEntity userEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        boolean flag = this.isAdmin();
        long userId=0L;

        if (flag){
            userId=userEntity.getUserId();

        }else {
            userId=userEntity.getCreateUserId();
        }
        ShopManage shopManage = shopManageMapper.selectOne(new LambdaQueryWrapper<ShopManage>()
                .eq(ShopManage::getUserId, userId));

        return R.ok("操作成功").put("shopMessage",shopManage);


    }

    @Override
    public R getShopList(Integer pageNum, Integer pageSize) {

        Page<ShopManage> page=new Page<>(pageNum,pageSize);
        Page<ShopManage> shopManageList=shopManageMapper.getShopList(page);
        return R.ok("操作成功").put("shopList",shopManageList);
    }


    //判断当前用户是不是管理员
    @Override
    public boolean isAdmin(){

        SysUserEntity userEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        List<Long> roleIdList = sysUserRoleDao.queryRoleIdList(userEntity.getUserId());

        boolean flag=false;
        if (roleIdList.size()>0){
            List<SysRoleEntity> sysRoleEntities = sysRoleService.listByIds(roleIdList);
            for (SysRoleEntity sysRoleEntity : sysRoleEntities) {

                if ("admin".equals(sysRoleEntity.getRoleName())){
                    flag=true;
                }

            }
        }else {
            flag=true;
        }


        return flag;

    }




}
